package smlab.utils
{
	import flash.display.DisplayObject;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.ColorTransform;
	
	/**
	 * 颜色辅助类
	 * @author simon
	 * 
	 */
	public class ColorUtils
	{
		public static const PCT_MAX:uint = 100;
		public static const RGB_MAX:uint = 256;
		public static const HUE_MAX:uint = 360;
		
		public function ColorUtils()
		{
		}
		
		/**
		 * 设置显示对象的颜色
		 * @param d			显示对象
		 * @param color		颜色值
		 * 
		 */
		public static function setColor(d:DisplayObject,color:uint=0xffffff):void{
			var c:ColorTransform=new ColorTransform();
			c.color=color;
			d.transform.colorTransform=c;
		}
		
		/**
		 * 将显示对象设置为灰色
		 * @param d
		 * 
		 */
		public static function setGrey(d:DisplayObject):void{
			if(d){
				var mat:Array = [0.3086,0.6094,0.082,0,0,0.3086,0.6094,0.082,0,0,0.3086,0.6094,0.082,0,0,0,0,0,1,0];
				var colorMat:ColorMatrixFilter = new ColorMatrixFilter(mat);
				d.filters = [colorMat];
			}
		}
		
		public static function HueToRGB(min:Number, max:Number, hue:Number) : Object
		{
			var _loc_4:Number = NaN;
			var _loc_5:Number = NaN;
			var _loc_6:Number = NaN;
			var _loc_7:int = 0;
			while (hue < 0)
			{
				
				hue = hue + HUE_MAX;
			}
			_loc_7 = int(hue / 60);
			_loc_6 = (hue - _loc_7 * 60) / 60;
			_loc_7 = _loc_7 % 6;
			_loc_4 = min + (max - min) * _loc_6;
			_loc_5 = max - (max - min) * _loc_6;
			switch(_loc_7)
			{
				case 0:
				{
					return {r:max, g:_loc_4, b:min};
				}
				case 1:
				{
					return {r:_loc_5, g:max, b:min};
				}
				case 2:
				{
					return {r:min, g:max, b:_loc_4};
				}
				case 3:
				{
					return {r:min, g:_loc_5, b:max};
				}
				case 4:
				{
					return {r:_loc_4, g:min, b:max};
				}
				case 5:
				{
					return {r:max, g:min, b:_loc_5};
				}
				default:
				{
					break;
				}
			}
			return null;
		}
		
		public static function RGBtoHSV(red:Number, green:Number, blue:Number) : Object
		{
			var _loc_4:Number = NaN;
			var _loc_5:Number = NaN;
			var _loc_6:Number = NaN;
			var _loc_7:Number = NaN;
			var _loc_8:Number = 0;
			_loc_5 = Math.max(red, Math.max(green, blue));
			_loc_4 = Math.min(red, Math.min(green, blue));
			if (_loc_5 == 0)
			{
				return {h:0, s:0, v:0};
			}
			_loc_7 = _loc_5;
			_loc_6 = (_loc_5 - _loc_4) / _loc_5;
			_loc_8 = RGBToHue(red, green, blue);
			return {h:_loc_8, s:_loc_6, v:_loc_7};
		}
		
		public static function HSVtoRGB(hue:Number, saturation:Number, value:Number) : Object
		{
			var _loc_4:* = (1 - saturation) * value;
			return HueToRGB(_loc_4, value, hue);
		}
		
		public static function getAlpha(c:uint) : uint
		{
			return c >> 24 & 255;
		}
		
		public static function HSVtoHSL(hue:Number, saturation:Number, value:Number) : Object
		{
			var _loc_4:* = HSVtoRGB(hue, saturation, value);
			return RGBtoHSL(_loc_4.r, _loc_4.g, _loc_4.b);
		}
		
		public static function getGreen(c:uint) : uint
		{
			return c >> 8 & 255;
		}
		
		public static function getColor24(r:uint, g:uint, b:uint) : uint
		{
			return r << 16 | g << 8 | b;
		}
		
		public static function getBlue(c:uint) : uint
		{
			return c & 255;
		}
		
		public static function HSLtoRGB(hue:Number, luminance:Number, saturation:Number) : Object
		{
			var _loc_4:Number = NaN;
			if (luminance < 0.5)
			{
				_loc_4 = saturation * luminance;
			}
			else
			{
				_loc_4 = saturation * (1 - luminance);
			}
			return HueToRGB(luminance - _loc_4, luminance + _loc_4, hue);
		}
		
		public static function getColor32(a:uint, r:uint, g:uint, b:uint) : uint
		{
			return a << 24 | r << 16 | g << 8 | b;
		}
		
		private static function center(a:Number, b:Number, c:Number) : Number
		{
			if (a > b)
			{
			}
			if (a > c)
			{
				if (b > c)
				{
					return b;
				}
				return c;
			}
			else
			{
				if (b > a)
				{
				}
				if (b > c)
				{
					if (a > c)
					{
						return a;
					}
					return c;
				}
				else
				{
					if (a > b)
					{
						return a;
					}
					return b;
				}
			}
		}
		
		public static function HSLtoHSV(hue:Number, luminance:Number, saturation:Number) : Object
		{
			var _loc_4:* = HSLtoRGB(hue, luminance, saturation);
			return RGBtoHSV(_loc_4.r, _loc_4.g, _loc_4.b);
		}
		
		public static function RGBToHue(red:Number, green:Number, blue:Number) : uint
		{
			var _loc_4:Number = NaN;
			var _loc_5:Number = NaN;
			var _loc_6:Number = NaN;
			var _loc_7:Number = NaN;
			var _loc_8:Number = NaN;
			_loc_7 = Math.max(red, Math.max(green, blue));
			_loc_5 = Math.min(red, Math.min(green, blue));
			if (_loc_7 - _loc_5 == 0)
			{
				return 0;
			}
			_loc_6 = center(red, green, blue);
			while (true)
			{
				
				if (red == _loc_7)
				{
					if (blue == _loc_5)
					{
						_loc_8 = 0;
					}
					else
					{
						_loc_8 = 5;
					}
					break;
				}
				if (green == _loc_7)
				{
					if (blue == _loc_5)
					{
						_loc_8 = 1;
					}
					else
					{
						_loc_8 = 2;
					}
					break;
				}
				if (red == _loc_5)
				{
					_loc_8 = 3;
				}
				else
				{
					_loc_8 = 4;
				}
				break;
			}
			if (_loc_8 % 2 == 0)
			{
				_loc_4 = _loc_6 - _loc_5;
			}
			else
			{
				_loc_4 = _loc_7 - _loc_6;
			}
			_loc_4 = _loc_4 / (_loc_7 - _loc_5);
			return 60 * (_loc_8 + _loc_4);
		}
		
		public static function RGBtoHSL(red:Number, green:Number, blue:Number) : Object
		{
			var _loc_4:Number = NaN;
			var _loc_5:Number = NaN;
			var _loc_6:Number = NaN;
			var _loc_7:Number = NaN;
			var _loc_8:Number = NaN;
			var _loc_9:Number = 0;
			_loc_5 = Math.max(red, Math.max(green, blue));
			_loc_4 = Math.min(red, Math.min(green, blue));
			_loc_7 = (_loc_4 + _loc_5) * 0.5;
			if (_loc_7 == 0)
			{
				return {h:_loc_9, l:0, s:1};
			}
			_loc_6 = (_loc_5 - _loc_4) * 0.5;
			if (_loc_7 < 0.5)
			{
				_loc_8 = _loc_6 / _loc_7;
			}
			else
			{
				_loc_8 = _loc_6 / (1 - _loc_7);
			}
			_loc_9 = RGBToHue(red, green, blue);
			return {h:_loc_9, l:_loc_7, s:_loc_8};
		}
		
		public static function getRed(c:uint) : uint
		{
			return c >> 16 & 255;
		}

	}
}